import React from 'react';
import {numberHandle} from '~/utils/number';
import CommonEchart from '../../../common/CommonEchart';

const precision = 2;

const tooltipCallback = (params = []) => {
    const tooltipList = params.map((chartItem, index) => {
        const {marker, name, seriesName, value} = chartItem;
        let result = '';
        if (index === 0) {
            result = `${name}<br/>${marker}${seriesName}: ${numberHandle(
                value,
                precision
            )}`;
        } else {
            result = `<br/>${marker}${seriesName}: ${numberHandle(
                value,
                precision
            )}`;
        }
        return result;
    });
    return tooltipList.join('');
};

const labelFormatter = (params = {}) => {
    const {value = ''} = params;
    return `${numberHandle(value, precision)}`;
};

// const newYAxis = [
//     {type: 'value', name: '实际转化金额'},
//     {type: 'value', name: '实际学生数'}
// ];

const extendOptions = {
    chartId: 'TrafficIncomeBarChart',
    // title: {
    //     text: '某站点用户访问来源',
    //     subtext: '纯属虚构',
    //     left: 'center'
    // },
    tooltip: {
        trigger: 'axis',
        formatter: tooltipCallback
    },
    legend: {
        // orient: 'vertical',
        left: 'left'
    },
    grid: {
        show: false,
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
    },
    yAxis: {
        newYAxis: [
            {type: 'value', name: ''},
            {type: 'value', name: ''}
        ],
        common: {
            splitLine: {show: false},
            axisLabel: {
                formatter: value => numberHandle(value)
            }
        }
    },
    series: {
        newYAxis: [
            {yAxisIndex: 0, name: '实际转化金额'},
            {yAxisIndex: 1, name: '实际学生数'}
        ],
        common: {
            barMaxWidth: 30,
            barGap: '50%',
            // label: {show: true, position: 'top', formatter: labelFormatter},
            emphasis: {
                focus: 'series'
            }
        }
    }
};

const TrafficIncomeBarChart = props => (
    <CommonEchart {...props} extendEchartsOptions={extendOptions} />
);

export default TrafficIncomeBarChart;
